﻿layui.use('layer', function () {
    var $ = layui.$;
    var layer = layui.layer;
    var util = layui.util;
    var form = layui.form;
    var table = layui.table;

    //供应商下拉框
    $.ajax({
        url: "/Rawmaterialtesting/SupplierxialaShow",
        type: "get",
        data: { token: localStorage.getItem("token") },
        success: function (data) {
            var data = JSON.parse(data);
            var options = '<option value="">请选择供应商</option>'; // 添加默认选项
            $.each(data.data, function (index, item) {
                options += '<option value="' + item.supplierName + '">' + item.supplierName + '</option>';
            });
            $("#vendor").html(options);
            form.render('select'); // 重新渲染下拉框
        },
        error: function () {
            console.log("获取数据失败");
            // 可以考虑显示一个错误消息给用户
        }
    });
    $(function () {
        Show();
    })
    ///添加
    var list = {
        rawMaterialtestingId:'',
        qualityNumber: '',
        qualityName: '',
        qualityDate: '',
        qualityPerson: '',
        vendor: '',
        ingredientNumber: '',
        ingredientName: '',
        unit: '',
        specification: '',
        rawMaterialBatchNumber: '',
        size: '',
        typeOfQC: '',
        gualitySchemeName: '',
        state: '',
        addName: '',
        addTime:''
    };
    function Show() {
        $.ajax({
            url: '/Rawmaterialtesting/RawmaterialtestingModel',
            type: 'get',
            data: { rawMaterialtestingId: localStorage.getItem("rawMaterialtestingId"), token: localStorage.getItem("token") },
            success: function (res) {
                var s = JSON.parse(res)
                console.log(s);
                plist = s.data.gualityProjectList
                $(s.data).each(function () {
                    $('#qualityNumber').val(this.qualityNumber)
                    $('#qualityName').val(this.qualityName)
                    $('#qualityDate').val(this.qualityDate.substring(0, 10))
                    $('#qualityPerson').val(this.qualityPerson)
                    $('#vendor').val(this.vendor)
                    $('#ingredientNumber').val(this.ingredientNumber)
                    $('#ingredientName').val(this.ingredientName)
                    $('#unit').val(this.unit)
                    $('#specification').val(this.specification)
                    $('#rawMaterialBatchNumber').val(this.rawMaterialBatchNumber)
                    $('#size').val(this.size)
                    $('#typeOfQC').val(this.typeOfQC)
                    $('#gualitySchemeName').val(this.gualitySchemeName)
                    $('#state').val(this.state)
                    list.rawMaterialtestingId = this.rawMaterialtestingId
                    list.qualityNumber = this.qualityNumber
                    list.qualityName = this.qualityName
                    list.qualityDate = this.qualityDate
                    list.qualityPerson = this.qualityPerson
                    list.vendor = this.vendor
                    list.ingredientNumber = this.ingredientNumber
                    list.ingredientName = this.ingredientName
                    list.unit = this.unit
                    list.specification = this.specification
                    list.rawMaterialBatchNumber = this.rawMaterialBatchNumber
                    list.size = this.size
                    list.typeOfQC = this.typeOfQC
                    list.gualitySchemeName = this.gualitySchemeName
                    list.state = this.state
                    list.addName = this.addName
                    list.addTime = this.addTime
                    form.render('select');
                })
                // 先进行表格的初始渲染
                table.render({
                    elem: '#RawmaterialtestingTable',  // 表格元素的选择器
                    page: {
                        limit: 5,
                        limits: [5, 8, 10]
                    },
                    toolbar: '#toolbarDemo',
                    defaultToolbar: ['exports'],
                    cols: [[
                        { type: 'checkbox', fixed: 'left' },
                        { field: 'gualityProjectName', title: '质检项目名称' },
                        { field: 'gualityProjectNumbering', title: '质检项目编号' },
                        { field: 'gualityProjectTool', title: '质检方法' },
                        { field: 'gualityProjectMethod', title: '质检工具' },
                        { field: 'gualityProjectType', title: '质检类型' },
                        { field: 'gualityProjectStandardValue', title: '标准值' },
                        { field: 'addTime', title: '上差值' },
                        { field: 'addName', title: '下差值' }
                    ]],
                    // 初始时可以先不加载数据，等待 ajax 请求成功后再更新
                    data: plist
                });
            }
        })
    }

    
    form.on('submit(demo-ddd)', function (data) {
        var checkStatus = table.checkStatus('RawmaterialtestingTable');
        console.log(checkStatus)
        if (checkStatus.length === 0) {
            layer.msg('请至少选择一条要添加的质检项目', { icon: 2 });
            return;
        }
        list.qualityNumber = $("#qualityNumber").val();
        list.qualityName = $("#qualityName").val();
        list.qualityDate = $("#qualityDate").val();
        list.qualityPerson = $("#qualityPerson").val();
        list.vendor = $("#vendor").val();
        list.ingredientNumber = $("#ingredientNumber").val();
        list.ingredientName = $("#ingredientName").val();
        list.unit = $("#unit").val();
        list.specification = $("#specification").val();
        list.size = $("#size").val();
        list.rawMaterialBatchNumber = $("#rawMaterialBatchNumber").val();
        list.gualitySchemeName = $("#gualitySchemeName").val();
        list.typeOfQC = $("#typeOfQC").val();
        list.state = $("#state").val();
        console.log(list)
        $.ajax({
            url: '/Rawmaterialtesting/RawmaterialtestingUpdate',
            type: 'post',
            data: { data: JSON.stringify(list), token: localStorage.getItem("token") },
            success: function (res) {
                var s = JSON.parse(res)
                console.log(res)
                if (s.data >= 1) {
                    layer.alert("修改成功");
                    setTimeout(function () {
                        window.location.href = "/Rawmaterialtesting/RawmaterialtestingShowView";
                    }, 2000);
                }
                else {
                    layer.alert("修改失败");
                }
            }
        })
        return false; // 阻止默认 form 跳转

    })
});
